package edu.CSCI.cdgoo_000.cameratest6;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;


public class MainActivity extends Activity {

    private WebView mWebView;
    //The Camera that the app will use to take the picture
    private Camera mCamera;
    // The preview for the picture that is not used but cannot really be gotten rid of
    public static CameraPreview mCameraPreview;
    // the picture that will be taken
    private static File picList = null;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //The WebView block that sets up the webview for the app
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com");
        mWebView.setWebViewClient(new MyWebViewClient());

        //Get the instance of the Camera
        mCamera = getCameraInstance();
        //Set up the camera preview
        mCameraPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mCameraPreview);

        Button captureButton = (Button) findViewById(R.id.button2);
        // Set up the onClick Listener to take the hidden photo
        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCamera == null){
                    Toast toast = Toast.makeText(getParent().getBaseContext(), "The camera is null", Toast.LENGTH_LONG);
                    toast.show();
                }else {
                   mCamera.takePicture(null, null, mPicture);


                }

            }
        });


    }
    //This class loads the url for the web page
    private class MyWebViewClient extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url){
            webview.loadUrl(url);
            return true;
        }
    }
    // When the back button is pressed, this method checks to see if their are any web pages that the view and go back to.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        // The pic taker key
        if((keyCode == KeyEvent.KEYCODE_MENU) && mWebView.canGoBack()) {
          //  takePic();

            mCamera.takePicture(null, null, mPicture);
            return true;

        }
        if((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
        {

            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }







  //This method checks to see if their is a camera and then calls it.
    private Camera getCameraInstance() {
        Camera camera = null;
        try {
            camera = Camera.open();
        } catch (Exception e) {
            // cannot get camera or does not exist
        }
        return camera;
    }

    //This method takes the data of the image taken by the camera and saves it to a file.
    Camera.PictureCallback mPicture = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera)  {

            String FILENAME = "hello_file.jpg";
            try{

                 picList = new File(getBaseContext().getFilesDir(),FILENAME);

                FileOutputStream fos =new  FileOutputStream(picList);

               fos.write(data);
               fos.close();





           } catch (FileNotFoundException e) {
                Log.e("Error1",""+picList.getAbsolutePath());

          } catch (IOException e) {

                Log.e("Error2",""+picList.exists() );
          }
        }

    };





// Getter for the picture the camera created

    public static CameraPreview getPic(){
        return mCameraPreview;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}
